import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SliverTabBarDelegate extends SliverPersistentHeaderDelegate {
  final Color backgroundColor;
  final TabBar tabBar;
  final Widget? endWidget;
  final Widget? titleWidget;
  final bool isEmpty;

  const SliverTabBarDelegate(
      {required this.tabBar,
      required this.backgroundColor,
      required this.isEmpty,
      this.endWidget,
      this.titleWidget});

  @override
  double get minExtent => tabBar.preferredSize.height;

  @override
  double get maxExtent => tabBar.preferredSize.height + 40 - (isEmpty ? 30 : 0);

  @override
  Widget build(context, shrinkOffset, overlapsContent) {
    // print('maxExtent ${}');
    return Container(
      color: backgroundColor,
      height: maxExtent,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          null == titleWidget ? Container() : titleWidget!,
          null == endWidget
              ? Padding(
                  padding: EdgeInsets.only(left: 20),
                  child: isEmpty ? Container() : tabBar)
              : Stack(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(left: 20),
                      child: isEmpty ? Container() : tabBar,
                    ),
                    Align(
                      alignment: Alignment.centerRight,
                      child: endWidget,
                    )
                  ],
                )
        ],
      ),
    );
  }

  @override
  bool shouldRebuild(SliverTabBarDelegate oldDelegate) {
    return tabBar != oldDelegate.tabBar;
  }
}
